package com.tg.app.activity.device.settings;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ListView;

import androidx.annotation.StringRes;

import com.appbase.custom.base.SettingData;
import com.tange.module.device.feature.DeviceFeatureObjectBox;
import com.tange.module.device.feature.DeviceFeature_;
import com.tg.app.R;
import com.tg.app.activity.base.DeviceSettingsBaseActivity;
import com.tg.app.activity.device.DeviceSettingsActivity;
import com.tg.app.adapter.SettingAdapter;
import com.tange.module.device.feature.DeviceFeature;
import com.tg.data.bean.DeviceSettingsInfo;
import com.tg.app.camera.Camera;
import com.tange.module.camera.hub.CameraHub;

import java.util.ArrayList;

import io.objectbox.Box;

public class SettingsGuideActivity extends DeviceSettingsBaseActivity {
    private ArrayList<SettingData> datas = new ArrayList<>();
    private SettingAdapter adapter;
    public final static String EXT_GUIDE_TYPE = "ext_guide_type";
    public final static int GUIDE_TYPE_MOTION_DETECTION = 0x01;
    public final static int GUIDE_TYPE_NOTIFICATION_INTERVAL = 0x02;
    private int mType;
    private DeviceFeature deviceFeature;
    private DeviceSettingsInfo mDeviceSettingsInfo;

    @Override
    protected void onSetupFailed() {

    }

    @Override
    public boolean shouldSetStatusBarColor() {
        return true;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);//remove title bar  即隐藏标题栏
        hideActionBar();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings_guide);
        mType = getIntent().getIntExtra(EXT_GUIDE_TYPE, 1);
        mDeviceSettingsInfo = getIntent().getParcelableExtra(DeviceSettingsActivity.EXT_DEVICE_INFO);
        initView();

        backClickEvent();
        if (mType == GUIDE_TYPE_MOTION_DETECTION) {
            Camera camera = mDeviceSettingsInfo == null ? null : CameraHub.getInstance().getCamera(mDeviceSettingsInfo.uuid);
            if (camera != null) {
                Box<DeviceFeature> featureBox = DeviceFeatureObjectBox.getDeviceFeature();
                deviceFeature = featureBox.query().equal(DeviceFeature_.uuid, camera.uid).build().findFirst();
            }
            if (deviceFeature != null && deviceFeature.supportDetectZone) {
                SettingData zone = new SettingData(getString(R.string.detection_zone), 0);
                datas.add(zone);
            }

            SettingData sensitivity = new SettingData(getString(R.string.detection_sensitivity), 0);
            datas.add(sensitivity);
        } else {
            SettingData notification = new SettingData(getString(R.string.settings_notification_interval), 0);
            datas.add(notification);
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    protected void initView() {
        @StringRes int rebid = R.string.new_notification;
        if (mType == GUIDE_TYPE_MOTION_DETECTION) {
            rebid = R.string.settings_motion_detection;
        }
        modifyToolBar(rebid);

        adapter = new SettingAdapter(datas, this);
        adapter.setCheckButton(false);
        ListView listView = findViewById(R.id.guide_list);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener((parent, view, position, id) -> {
            Intent intent = new Intent();
            if (mDeviceSettingsInfo != null) {
                intent.putExtra(Camera.EXT_UUID, mDeviceSettingsInfo.uuid);
            }

            startActivity(intent);
        });
    }
}
